<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\Barn;
use app\model\BarnFeedPlan;
use think\facade\Db;

class BarnLogic
{
    protected Barn $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new Barn();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @author: zj
     * @param int $id
     * @param array $param
     * @time 2024/03/11 16:27:54
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addEdit(int $id, array $param)
    {
        Db::startTrans();
        try {
            $exist = $this->model->checkOutId($this->factory_id,$id);

            if ($exist) { // 猪栋已存在，执行编辑
                $rule = [
                    'Name' => 'max:50',
                    'IPAddress' => 'max:20',
                    'FeedBaseTime' => '>:0',
                    'FeedPreDayId' => '>=:0',
                ];
                validate($rule)->check($param);

                $updateData = [];
                if ($param['Name']) {
                    $updateData['name'] = $param['Name'];
                }
                if ($param['IPAddress']) {
                    $updateData['ip'] = $param['IPAddress'];
                }
                if ($param['FeedBaseTime']) {
                    $updateData['feed_base_time'] = $param['FeedBaseTime'];
                }
                if (isset($param['FeedPreDayId'])) {
                    $updateData['feed_plan_id'] = $param['FeedPreDayId'];
                }
                $updateData['update_record_time'] = $param['RecordTime'];
                $updateData['update_time'] = request()->time();

                $this->model->where('out_id',$id)
                    ->where('delete_time',$this->model::IS_DELETE_NO)
                    ->update($updateData);
            }else{ // 猪栋不存在，执行新增
                $rule = [
                    'Name' => 'require|max:50',
                    'IPAddress' => 'require|max:20',
                    'FeedBaseTime' => 'require|>:0',
                    'FeedPreDayId' => 'require|>=:0',
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $id,
                    'factory_id' => $this->factory_id,
                    'name' => $param['Name'],
                    'ip' => $param['IPAddress'],
                    'feed_base_time' => $param['FeedBaseTime'],
                    'feed_plan_id' => $param['FeedPreDayId'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time(),
                ]);
            }

            Db::commit();
        }catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }
}